package manager;

import graphic.complex.Car;
import graphic.complex.Town;
import graphic.primitives.town.Bench;
import graphic.complex.Person;
import graphic.primitives.town.Railing;
import graphic.scene.Stage1;
import graphic.scene.Stage3;
import graphic.window.MainWindow;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Manager extends Controller
{
	public Manager()
	{
		super();

		this.keyboard		= new boolean[Keys.values().length];
		this.car			= new Car(50, 250, 265);
		this.drawingDone	= false;
	
		for (int i = 0; i < Keys.values().length; i++)
		{
			keyboard[i] = false;
		}
		
		this.mainWindow.addKeyListener(new KeyListener()
		{
			@Override
			public void keyPressed(KeyEvent ke)
			{
				switch (ke.getKeyCode())
				{
					case KeyEvent.VK_SPACE:
						keyboard[Keys.SPACE.ordinal()] = true;
						break;
						
					case KeyEvent.VK_CONTROL:
						keyboard[Keys.CONTROL.ordinal()] = true;
						break;
						
					case KeyEvent.VK_ALT:
						keyboard[Keys.ALT.ordinal()] = true;
						break;
						
					case KeyEvent.VK_UP:
						keyboard[Keys.UP.ordinal()] = true;
						break;
						
					case KeyEvent.VK_DOWN:
						keyboard[Keys.DOWN.ordinal()] = true;
						break;
						
					case KeyEvent.VK_LEFT:
						keyboard[Keys.LEFT.ordinal()] = true;
						break;
						
					case KeyEvent.VK_RIGHT:
						keyboard[Keys.RIGHT.ordinal()] = true;
						break;
						
					case KeyEvent.VK_F1:
						car.switchFrontLight();
						break;
						
					case KeyEvent.VK_F2:
						car.switchBackLight();
						break;
						
					case KeyEvent.VK_F9:
						car.pullDownLeftWindow();
						break;
						
					case KeyEvent.VK_F10:
						car.pullUpLeftWindow();
						break;
						
					case KeyEvent.VK_F11:
						car.pullDownRightWindow();
						break;
						
					case KeyEvent.VK_F12:
						car.pullUpRightWindow();
						break;
				}
			}

			@Override
			public void keyReleased(KeyEvent ke)
			{
				switch (ke.getKeyCode())
				{
					case KeyEvent.VK_SPACE:
						keyboard[Keys.SPACE.ordinal()] = false;
						break;
						
					case KeyEvent.VK_CONTROL:
						keyboard[Keys.CONTROL.ordinal()] = false;
						break;
						
					case KeyEvent.VK_ALT:
						keyboard[Keys.ALT.ordinal()] = false;
						break;
						
					case KeyEvent.VK_UP:
						keyboard[Keys.UP.ordinal()] = false;
						break;
						
					case KeyEvent.VK_DOWN:
						keyboard[Keys.DOWN.ordinal()] = false;
						break;
						
					case KeyEvent.VK_LEFT:
						keyboard[Keys.LEFT.ordinal()] = false;
						break;
						
					case KeyEvent.VK_RIGHT:
						keyboard[Keys.RIGHT.ordinal()] = false;
						break;
				}
			}

			@Override
			public void keyTyped(KeyEvent ke)
			{
				//
			}
		});
		start(); 
		
		this.town = new Town(0, 210, 1000);
	}
	
	@Override
	protected void redraw()
	{
		this.drawingDone = false;

		mainWindow.stopUpdate();
		mainWindow.clear();

		new Stage1().redraw();

		if (town != null)
		{
			town.redraw();
		}

		new Stage3().redraw();
		
		if (isKeyPressed(Keys.RIGHT))
		{
			car.moveAtXAxis(10);
			car.rotateWheel(5);
			car.enableSmoke();
		}
		else
		if (isKeyPressed(Keys.LEFT))
		{
			car.moveAtXAxis(-10);
			car.rotateWheel(-5);
			car.enableSmoke();
		}
		
		if (isKeyPressed(Keys.DOWN))
		{
			car.changeDirection(true);
		}
		else
		if (isKeyPressed(Keys.UP))
		{
			car.changeDirection(false);
		}
		
		if (isKeyPressed(Keys.SPACE))
		{
			car.moveAtYAxis(-5);
		}
	
		if (isKeyPressed(Keys.CONTROL))
		{
			car.moveAtYAxis(5);
		}
		
		car.redraw();
		new Railing(0, 350, 1000, 35).redraw();

		new Person(100, 420).redraw();
		new Bench(250, 380).redraw();
		new Bench(380, 380).redraw();
		
		mainWindow.startUpdate();
		car.disableSmoke();
		
		drawingDone = true;
	}
	
	private boolean isKeyPressed(Keys k)
	{
		return keyboard[k.ordinal()];
	}
	
	@Override
	protected boolean isDrawingDone()
	{
		return drawingDone;
	}

	@Override
	protected void onTimerEvent()
	{
		//
	}	
	
	private Car			car;
	private Town		town;
	private boolean		keyboard[];
	private boolean		drawingDone;

	private int			ticksCounter = 0;
	
	enum Keys
	{
		SPACE,
		CONTROL,
		ALT,
		UP,
		DOWN,
		LEFT,
		RIGHT
	}
}
